package com.aopcinemas.visitor.behaviour;

import jade.content.Predicate;
import jade.content.lang.Codec.CodecException;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.content.onto.basic.FalseProposition;
import jade.content.onto.basic.TrueProposition;
import jade.core.Agent;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.RefuseException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.AchieveREResponder;

import com.aopcinemas.cinema.ontology.CinemaOntology;
import com.aopcinemas.cinema.ontology.OrderRequest;
import com.aopcinemas.visitor.VisitorAgent;
import com.aopcinemas.visitor.VisitorUtils;
import com.aopcinemas.visitor.model.Campaign;
import com.aopcinemas.visitor.ontology.CampaignRequest;

public class CampaignRequestInfoBehavior extends AchieveREResponder {

    private static final long serialVersionUID = 1L;

    public CampaignRequestInfoBehavior(Agent a, MessageTemplate mt) {
        super(a, mt);
    }

    @Override
    protected ACLMessage handleRequest(ACLMessage request) throws NotUnderstoodException, RefuseException {
        VisitorAgent agent = (VisitorAgent) myAgent;
        try {
            Action action = (Action) myAgent.getContentManager().extractContent(request);
            System.out.println("CampaignRequestInfoBehavior: " + action.getAction());
            CampaignRequest campaignRequest = (CampaignRequest) action.getAction();

            ACLMessage reply = request.createReply();
            reply.setPerformative(ACLMessage.INFORM);
            Campaign campaign = agent.getCampaign();
            if (campaign == null || campaign != null && campaign.isAllParticipants() || campaign != null && campaign.isInParticipants(request.getSender())) {
                reply.setPerformative(ACLMessage.NOT_UNDERSTOOD);
                return reply;
            }

            boolean approved = agent.campaignRequestReceived(campaignRequest);

            Predicate answer = approved ? new TrueProposition() : new FalseProposition();
            myAgent.getContentManager().fillContent(reply, answer);

            if (approved) {

                campaign.getParticipants().add(request.getSender());
                if (campaign.isAllParticipants()) {
                    VisitorUtils.unregisterCampaign(agent);
                    agent.notifyOrderCampaign();

                    ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
                    message.setLanguage(agent.getCodec().getName());
                    message.setOntology(CinemaOntology.getInstance().getName());
                    message.addReceiver(campaign.getSchedule().getCinema());

                    OrderRequest orderRequest = new OrderRequest();
                    orderRequest.setNumberOfSessions(campaign.getNumberOfParticipant());
                    orderRequest.setPerson(agent.getPerson());
                    orderRequest.setSchedule(campaign.getSchedule());

                    myAgent.getContentManager().fillContent(message, new Action(myAgent.getAID(), orderRequest));
                    myAgent.addBehaviour(new OrderRequestInitiator(myAgent, message));
                }

            }

            return reply;
        } catch (CodecException e) {
            e.printStackTrace();
        } catch (OntologyException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("Something is wrong");
    }
}
